package com.carefree.system.modules.admin.controller;

import com.carefree.common.core.PageUtils;
import com.carefree.common.core.Result;
import com.carefree.common.emus.BusinessTypeEnum;
import com.carefree.common.emus.OperatorTypeEnum;
import com.carefree.common.valid.AddGroup;
import com.carefree.common.valid.UpdateGroup;
import com.carefree.system.common.annotation.SystemLog;
import com.carefree.system.modules.admin.entity.UserEntity;
import com.carefree.system.modules.admin.service.UserService;
import com.carefree.system.modules.admin.vo.AssginRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;


/**
 * 用户
 *
 * @author tengbin
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/sys/user")
public class UserController {

    @Resource
    private UserService userService;


    /**
     * 列表
     *
     * @param params 分页参数
     * @return
     */
    @SystemLog(value = "分页查询用户信息", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "分页查询用户信息")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('btn:user:list')")
    public Result<PageUtils> list(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.queryPageForVo(params);
        return Result.success(page);
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @SystemLog(value = "根据id查询用户信息", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据id查询用户信息")
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('btn:user:list')")
    public Result<UserEntity> info(@PathVariable("id") Long id) {
        UserEntity user = userService.getById(id);
        user.setPassword("");
        return Result.success(user);
    }

    /**
     * 保存
     *
     * @param User 用户
     * @return
     */
    @SystemLog(value = "保存用户信息", businessType = BusinessTypeEnum.INSERT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "保存用户信息")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('btn:user:add')")
    public Result<Object> save(@Validated({AddGroup.class}) @RequestBody UserEntity user) {
        return userService.saveUser(user) ? Result.ok() : Result.error();
    }

    /**
     * 修改
     *
     * @param User 用户
     * @return
     */
    @SystemLog(value = "修改用户信息", businessType = BusinessTypeEnum.UPDATE, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "修改用户信息")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('btn:user:update')")
    public Result<Object> update(@Validated({UpdateGroup.class}) @RequestBody UserEntity user) {
        return userService.updateUser(user) ? Result.ok() : Result.error();
    }


    /**
     * 启用或禁用
     *
     * @param id
     * @return
     */
    @SystemLog(value = "根据id启用或禁用用户", businessType = BusinessTypeEnum.STATUS, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据id启用或禁用用户")
    @GetMapping("/active/{id}/{status}")
    @PreAuthorize("hasAuthority('btn:user:enable')")
    public Result<Object> activeOrInActiveById(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return userService.activeOrInActiveById(id, status) ? Result.ok() : Result.error();
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @SystemLog(value = "根据id批量删除用户信息", businessType = BusinessTypeEnum.DELETE, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation(value = "根据id批量删除用户信息")
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('btn:user:remove')")
    public Result<Object> delete(@RequestBody Long[] ids) {
        return userService.removeByIds(Arrays.asList(ids)) ? Result.ok() : Result.error();
    }


    /**
     * 为指定用户id分配角色
     *
     * @param assginRoleVo 自定义分配角色vo
     * @return
     */
    @SystemLog(value = "为指定用户id分配角色", businessType = BusinessTypeEnum.SELECT, operatorType = OperatorTypeEnum.MANAGE)
    @ApiOperation("为指定用户id分配角色")
    @PostMapping("/assign")
    @PreAuthorize("hasAuthority('btn:role:assignRole')")
    public Result<Object> doAssignRoles(@RequestBody AssginRoleVo assginRoleVo) {
        return userService.assignRolesByUserId(assginRoleVo) ? Result.ok() : Result.error();
    }


}
